﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemiCASE.Business;

public partial class UserList : SemiCASEBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            JavascriptHelper.PopUpWindowEventiEkle(HyperlinkNewUser, "~/PopUp/CreateUser.aspx", 500, 250, true);
            LoadUsers();
        }
    }

    private void LoadUsers()
    {
        DataTable dt = new LoginBS().ListUser();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["SiraNo"] = (i + 1).ToString();
        }

        if (dt.Rows.Count > 0)
        {
            gvUsers.DataSource = dt;
            gvUsers.DataBind();
            LabelRowCount.Text = "Row Count: " + dt.Rows.Count.ToString();
            Session["UserList"] = dt;
        }
        else
        {
            gvUsers.DataSource = null;
            gvUsers.DataBind();
            LabelRowCount.Text = String.Empty;
        }
    }

    protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        string UserID = gvUsers.DataKeys[e.Row.RowIndex].Values["UserID"].ToString();
        string queryString = "?UserID=" + UserID;

        HyperLink hyperLinkGuncelle = (HyperLink)e.Row.FindControl("HyperLinkUpdate");
        JavascriptHelper.PopUpWindowEventiEkle(hyperLinkGuncelle, "~/PopUp/CreateUser.aspx" + queryString, 500, 250, true);
    }

    protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteUser")
        {
            DeleteUser(e.CommandArgument.ToString());
        }
    }

    private void DeleteUser(string UserID)
    {
        try
        {
            new LoginBS().DeleteUser(new Guid(UserID));
            LoadUsers();
            MessageBox("User deleted successfully", SemiCASE.Interfaces.MesajTuruEnum.Bilgi);
        }
        catch (Exception exc)
        {
            MessageBox("User couldn't be deleted!", SemiCASE.Interfaces.MesajTuruEnum.Hata);
        }
    }
}